package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect1;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.ElasticQuesService;

/**
 * 获取学生填空或简答试题作答
 */
@CrossOrigin
@RestController
public class Workcorrect1GetStuQues34AnswerController {

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @Autowired
    HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @Autowired
    ElasticQuesService elasticQuesService;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workcorrect1/stu/ques34/id/answer/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String studentId, //
            @RequestParam String quesId, //
            @RequestParam String homeworkTaskReportQuesId, RespModel respModel) throws ParseException {

        // 获得原题
        try {
            respModel.ques = elasticQuesService.findByEsQuesId(quesId);
        } catch (Exception e) {
            respModel.ques = null;
        }

        // 获取学生的作答
        HomeworkTaskReportQues homeworkTaskReportQues = homeworkTaskReportQuesRepository
                .findById(homeworkTaskReportQuesId);

        // return
        respModel.quesId = homeworkTaskReportQues.getQuesId();
        respModel.homeworkTaskReportQuesId = homeworkTaskReportQues.getId();
        respModel.duration = homeworkTaskReportQues.getDuration();
        respModel.mark = homeworkTaskReportQues.getMark();
        respModel.order = homeworkTaskReportQues.getOrder();
        respModel.questionType = homeworkTaskReportQues.getQuestionType();
        respModel.answerText = homeworkTaskReportQues.getAnswerText();
        respModel.answerImgs = homeworkTaskReportQues.getAnswerImgs();
        respModel.knowIdLst = homeworkTaskReportQues.getKnowIdLst();
        respModel.knowNameLst = homeworkTaskReportQues.getKnowNameLst();
        respModel.wrongKnowIdLst = homeworkTaskReportQues.getWrongKnowIdLst();
        respModel.wrongKnowNameLst = homeworkTaskReportQues.getWrongKnowNameLst();
        respModel.exactnessKnowIdLst = homeworkTaskReportQues.getExactnessKnowIdLst();
        respModel.exactnessKnowNameLst = homeworkTaskReportQues.getExactnessKnowNameLst();
        if ((respModel.answerText != null && respModel.answerText.length() > 0)
                || (respModel.answerImgs != null && respModel.answerImgs.size() > 0)) {
            respModel.isStuAnswer = true;
        }
        return respModel;
    }

    /** 响应 **/
    protected static class RespModel extends YxyApiRespModel {
        public Object ques; // 试题 Object
        public String quesId; // 试题Id
        public String homeworkTaskReportQuesId; // homeworkTaskReportQues Id
        public Integer duration; // 做题时长
        public Integer mark; // 评分
        public Integer order; // 试题序号
        public String questionType; // 试题类型
        public String answerText; // 主观题 题目的回答
        public List<String> answerImgs; // 主观题 上传的图片
        public List<Integer> knowIdLst; // 知识点:ID
        public List<String> knowNameLst; // 知识点:Name
        public List<Integer> wrongKnowIdLst; // 勾选错误知识点:ID
        public List<String> wrongKnowNameLst; // 勾选错误知识点:Name
        public List<Integer> exactnessKnowIdLst; // 勾选正确知识点:ID
        public List<String> exactnessKnowNameLst; // 勾选正确知识点:Name
        public boolean isStuAnswer = false; // 学生是否作答
    }
}
